<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/4/10
 * Time: 下午10:00
 */

namespace app\admin\controller;


use app\admin\model;

class Login extends Base
{
    /**
     * 登陆
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isPost()) {

            $email = input('post.email');
            $password = input('post.password');

            $adminMember = new model\AdminMember();

            $userinfo = $adminMember->getUserinfoByEmail($email);

            if (empty($userinfo)) {
                $this->error('该用户不存在或已被禁用');
            }

            if ($userinfo->password != $adminMember::getEncryptPassword($password)) {
                $this->error('密码输入错误');
            }

            $adminMember->autoLogin($userinfo->toArray());

            $this->success('登陆成功', url('admin/Index/index'));
        }

        return $this->fetch('public/login');
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $adminMember = new model\AdminMember();

        $adminMember->autoLogout();

        $this->redirect(url('admin/Login'));
    }
}